﻿using System.Collections.Generic;
using System.Threading;
using PPI.Client.Logic.Distribution;
using PPI.Entities.Distribution;
using PPI.Entities.Inspections;
using PPI.Entities.Lookup;
using ENote=PPI.Entities.Distribution.ENote;

namespace PPI.Client
{
    public sealed class LCacheDistributionDataSingleton
    {
        #region - Properties -

        private static List<EXMissionStructures_PoleTopType> m_Tops;
        private static List<EPhase> m_Phases;
        private static List<EXMissionStructures_PoleClass> m_Classes;
        private static List<EXMissionStructures_PoleSpecies> m_Species;
        private static List<EXMissionStructures_PoleTreatment> m_Treatments;
        private static List<EXMissionStructures_PoleLength> m_Lengths;
        private static List<EInspectionTypes> m_Inspections;
        private static List<EResultType> m_ResultsTypes;
        private static List<ENote> m_Notes;
        private static List<ENote> m_MeterNotes;
        private static List<EMap> m_Maps;
        private static List<EDistributionLookUp> m_KVs;

        public List<EXMissionStructures_PoleTopType> Tops
        {
            get
            {
                while (thread.IsAlive) { };

                return m_Tops;        
            } 
            set
            {
                m_Tops = value;
            }
        }

        public List<EPhase> Phases
        {
            get
            {
                while (thread.IsAlive) { };
                
                return m_Phases;
            }
            set
            {
                m_Phases = value;
            }
        }

        public List<EXMissionStructures_PoleClass> Classes
        {
            get
            {
                while (thread.IsAlive) { };

                return m_Classes;
            }
            set
            {
                m_Classes = value;
            }
        }

        public List<EXMissionStructures_PoleSpecies> Species
        {
            get
            {
                while (thread1.IsAlive) { };

                return m_Species;
            }
            set
            {
                m_Species = value;
            }
        }

        public List<EXMissionStructures_PoleTreatment> Treatments
        {
            get
            {
                while (thread1.IsAlive) { };

                return m_Treatments;
            }
            set
            {
                m_Treatments = value;
            }
        }

        public List<EXMissionStructures_PoleLength> Lengths
        {
            get
            {
                while (thread1.IsAlive) { };

                return m_Lengths;
            }
            set
            {
                m_Lengths = value;
            }
        }

        public List<EInspectionTypes> Inspections
        {
            get
            {
                while (thread2.IsAlive) { };

                return m_Inspections;
            }
            set
            {
                m_Inspections = value;
            }
        }

        public List<EResultType> ResultsTypes
        {
            get
            {
                while (thread2.IsAlive) { };

                return m_ResultsTypes;
            }
            set
            {
                m_ResultsTypes = value;
            }
        }

        public List<ENote> Notes
        {
            get
            {
                while (thread2.IsAlive) { };

                return m_Notes ;
            }
            set
            {
                m_Notes = value;
            }
        }

        public List<ENote> MeterNotes
        {
            get
            {
                while (thread2.IsAlive) { };

                return m_MeterNotes;
            }
            set
            {
                m_MeterNotes = value;
            }
        }

        public List<EMap> Maps
        {
            get
            {
                while (thread2.IsAlive) { };

                return m_Maps;
            }
            set
            {
                m_Maps = value;
            }
        }

        public List<EDistributionLookUp> DistributionLookUps
        {
            get
            {
                while (thread2.IsAlive) { };

                return m_KVs;
            }
            set
            {
                m_KVs = value;
            }
        }

        #endregion

        private static LCacheDistributionDataSingleton m_instance;
        static readonly object padlock = new object();
        private Thread thread;
        private Thread thread1;
        private Thread thread2;

        public static LCacheDistributionDataSingleton Instance
        {
            get
            {
                lock (padlock)
                {
                    if (m_instance == null)
                        m_instance = new LCacheDistributionDataSingleton();

                    return m_instance;
                }
            }
        }
        
        private LCacheDistributionDataSingleton()
        {
            LoadData();
        }

        private void LoadData()
        {
            thread = new Thread(Load);
            thread.Start();

            thread1 = new Thread(Load1);
            thread1.Start();

            thread2 = new Thread(Load2);
            thread2.Start();

            /*
            while (thread.IsAlive){}
            while (thread1.IsAlive) { }
            while (thread2.IsAlive) { }
            while (thread3.IsAlive) { }
            */
        }

        private static void Load()
        {
            m_Tops = LTop.SelectAllIdName();
            m_Phases = LPhase.SelectAllIdName();
            m_Classes = LClass.SelectAllIdName();
        }

        private static void Load1()
        {
            m_Species = LSpecie.SelectAllIdName();
            m_Treatments = LTreatment.SelectAllIdName();
            m_Lengths = LLength.SelectAllIdName();
        }

        private static void Load2()
        {
            m_Maps = LMap.SelectAllIdName();
            m_Inspections = LInspectionResult.SelectAllIdName();
            m_ResultsTypes = LResultType.SelectAllIdName();
            m_Notes = LNote.SelectAllIdName();
            m_MeterNotes = LMeterNote.SelectAllIdName();
            m_KVs = LDistributionLookUp.SelectAllIdName();
        }

        //private static void Load()
        //{
        //    m_Tops = LTop.SelectAllIdName();
        //    m_Phases = LPhase.SelectAllIdName();
        //    m_Classes = LClass.SelectAllIdName();
        //}

        //private static void Load1()
        //{
        //    m_Species = LSpecie.SelectAllIdName();
        //    m_Treatments = LTreatment.SelectAllIdName();
        //    m_Lengths = LLength.SelectAllIdName();
        //}

        //private static void Load2()
        //{
        //    m_Maps = LMap.SelectAllIdName();
        //    m_Inspections = LInspectionResult.SelectAllIdName();
        //    m_ResultsTypes = LResultType.SelectAllIdName();
        //}

        //private static void Load3()
        //{
        //    m_Notes = LNote.SelectAllIdName();
        //    m_MeterNotes = LMeterNote.SelectAllIdName();
            
        //}
    }
}
